/*
 * @Author       : xiaoxiaoguai
 * @Date         : 2025-05-20 16:42:13
 * @LastEditors  : Please set LastEditors
 * @LastEditTime : 2025-07-26 14:17:17
 * @Description  : 
 */
import stringify from '@/utils/querystring'
import { getTenantIdByAppId } from '@/api/auth'
import router from './router'
import cookie from './cookie'

export const handleLoginFailure = () => {
  // router.replace({
  //   path: '/pages/login/login',
  // })
  uni.navigateTo({
    url: '/pages/components/pages/login/login',
  })
  uni.removeStorageSync("accessToken");
  uni.removeStorageSync("userinfo");
}

export function parseUrl(location) {
  if (typeof location === 'string') return location
  const { url, query } = location

  const queryStr = stringify(query)

  if (!queryStr) {
    return url
  }

  return `${url}?${queryStr}`
}

const toAuth = () => {
  uni.showToast({
    title: '暂未开放',
    icon: 'none',
    duration: 2000,
  })
}

export default {
  install: (app, options) => {
    // 在这里编写插件代码
    // 注入一个全局可用的 $translate() 方法
    app.config.globalProperties.$yrouter = router
    app.config.globalProperties.$cookie = cookie
    app.config.globalProperties.$toAuth = toAuth
    app.config.globalProperties.$onClickLeft = () => {
      router.back()
      //uni.navigateBack()
      //const mypage = getCurrentPages()
      //console.log('mypage:',mypage)
    }
    // app.config.globalProperties.$getAppId = () => {
    //   // 没用了
    //   // 至日拉面
    //   return 'wxf44f4c333708e84f'
    //   // 莫热店
    //   // return 'wx98af98ee9dac2121'
    // }
    app.config.globalProperties.$getTenantIdByAppId = async () => {
      // if (!uni.getStorageSync('storeTenantId')) {
        const data = await getTenantIdByAppId()
        uni.setStorageSync('storeTenantId', data)
      // }
    }

    // #ifdef H5
    app.config.globalProperties.$platform = 'h5'
    // #endif

    // #ifdef APP-PLUS
    // app端
    app.config.globalProperties.$platform = 'app'
    // #endif

    // #ifdef MP-WEIXIN
    app.config.globalProperties.$platform = 'routine'
    // #endif
  },
}
